package cn.zcf.pet.controller;

import cn.zcf.basic.utils.JsonResult;
import cn.zcf.basic.utils.LoginContext;
import cn.zcf.basic.utils.PageList;
import cn.zcf.pet.domain.SearchMessage;
import cn.zcf.pet.dto.SearchDto;
import cn.zcf.pet.query.SearchMessageQuery;
import cn.zcf.pet.service.ISearchMessageService;
import cn.zcf.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class SearchMessageController {
    @Autowired
    private ISearchMessageService service;
    @GetMapping
    @ApiOperation(value = "查询全部" )
    public List<SearchMessage> loadAll(){
        return service.loadAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public SearchMessage loadById(@PathVariable("id") Long id){
        return service.loadById(id);
    }
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    public JsonResult addOrUpt(@RequestBody SearchMessage pet){
        try{
            if(pet.getId()==null){
                service.add(pet);
            }
            else {
                service.update(pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
    @PostMapping
    @ApiOperation(value = "分页查询" )
    public PageList<SearchMessage> pageQuery(@RequestBody SearchMessageQuery query){
        try {
            return service.pageQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMessage>();
        }

    }
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            service.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("删除失败");
        }
    }
    @PostMapping("/publish")
    public JsonResult publish(@RequestBody SearchMessage s, HttpServletRequest request){
        try {
            service.publish(s,request);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙");
        }

    }
    @PostMapping("/toHandle")
    @ApiOperation(value = "展示待处理寻主信息" )
    public PageList<SearchMessage> toHandle(@RequestBody SearchMessageQuery query,HttpServletRequest request){
        try {
            return service.toHandle(query,request);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMessage>();
        }

    }
    @GetMapping("/reject/{id}")
    public JsonResult reject(@PathVariable("id") Long id){
        try {
            service.reject(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("网络繁忙");
        }
    }
    @PostMapping("/accept")
    public JsonResult pageQuery(@RequestBody SearchDto dto,HttpServletRequest request){
        try {
            service.accept(dto,request);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }

    }
}